package abmb.utils;


import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Locale;

/**
 * This class contains methods for printing part of html pages.
 * @author Andrea Balboni
 * @version 1.0
 */
public class PageParts {
	public String printFollowedPostTitles(
			ArrayList<MBPost> postList) {
		String postTitles="";
		postTitles+="<section>\n";
		if(postList.isEmpty()){
			postTitles+="No posts found, to follow a user click <a href=\""+PageLinks.getFollowLink()+"\">here</a>\n";
		}
		else{
			for(MBPost post :postList){
				postTitles+=(printPostTitle(post));
			}
		}
		postTitles+="</section>\n";
		return postTitles;

	}
	public String printFooterSection(){
		String HTMLFooter="";
		HTMLFooter+="<footer>\n";
		HTMLFooter+="<dl>\n";
		HTMLFooter+="<dt>Created by</dt>\n";
		HTMLFooter+="<dd><address><a href=\"mailto:44420@studenti.unimore.it\">Andrea Balboni</a></address>\n";
		HTMLFooter+="</dl>\n";
		HTMLFooter+="<small>CSS based on <a href=\"http://www.html.it/guide/esempi/html5/esempi/progetto_blog/stili/progetto1.html\">this site</a> with some changes</small>\n";
		HTMLFooter+="</footer>\n";
		return HTMLFooter;
	}
	public String printHTMLFooter(){
		String HTMLFooter="";
		HTMLFooter+="</body>\n";
		HTMLFooter+="</html>\n";
		return HTMLFooter;
	}

	public String printHTMLHeader(String title,boolean loggedin){
		String HTMLHeader="";
		HTMLHeader+="<!doctype html>\n";

		HTMLHeader+="<html lang=\"en\">\n";
		HTMLHeader+="<head>\n";
		HTMLHeader+="<meta charset=\"utf-8\">\n";
		HTMLHeader+="<title> AB-Microblog </title>\n";
		HTMLHeader+="<link rel=\"stylesheet\" href=\"monitor.css\" media=\"screen\">\n";
		HTMLHeader+="</head>\n";
		HTMLHeader+="<body>\n";
		HTMLHeader+="<header>\n";
		HTMLHeader+="<hgroup>\n";
		HTMLHeader+="<h1>AB-Microblog</h1>\n";
		HTMLHeader+="<h2>A simple microblogging system</h2>\n";
		HTMLHeader+="</hgroup>\n";
		HTMLHeader+=printNavigationSection(loggedin).toString();
		HTMLHeader+="</header>\n";
		return HTMLHeader;
	}

	public String printNavigationSection(boolean loggedin){
		ArrayList<String> navLinks=PageLinks.getNavLinks(loggedin);
		String navSection="";
		navSection+="<nav>\n";
		navSection+="<h1>Navigation links</h1>\n";
		navSection+="<ul>\n";
		for(String link:navLinks){
			navSection+="<li>"+link+"</li>\n";
		}
		navSection+="</ul>\n";
		navSection+="</nav>\n";
		return navSection;

	}

	public String printNoLoginMessage(){
		String message="";
		message+="<section>\n";
		message+="<p>You are not logged in, please login <a href=\""+PageLinks.getLoginLink()+"\">here</a></p>\n";
		message+="</section>\n";
		return message;
	}

	public String printPost(MBPost post, int reqUserID){
		DateFormat df=DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.MEDIUM, new Locale(System.getProperty("user.language")));
		String sPost="";
		sPost+="<article>\n";
		sPost+="<header>\n";
		sPost+="\t<time datetime=\""+new java.sql.Date(post.postDate().getTime())+"\" pubdate>"+df.format(new java.util.Date(post.postDate().getTime()))+"</time>\n";
		sPost+="\t<h1>"+post.title()+"</h1>\n";
		sPost+="</header>\n";
		sPost+="\t\t<p>\n";
		sPost+="\t\t\t"+post.body()+"\n";
		sPost+="\t\t</p>\n";
		sPost+="\t\t<footer><dl>\n";
		sPost+="\t\t\t <dt>Author:    </dt>\n";
		sPost+="\t\t\t<dd><a href=\"index?keyword="+post.username()+"\">"+post.author()+"</a></dd>\n";
		sPost+="\t\t\t<dt>Tags:      </dt>\n";
		for(int i=0;i<post.tags().size();++i){
			sPost+="\t\t\t<dd><a href=\""+PageLinks.getLink("index")+"?tagkeyword="+post.tags().get(i)+"\">"+post.tags().get(i)+"</a></dd>\n";
		}
		sPost+="\t\t</dl>\n";
		if(post.userID()==reqUserID){
			sPost+="\t\t<dl>\n";
			sPost+="\t\t\t<dt>Actions:		</dt>";
			sPost+="\t\t<dd><a href=\"postmessage?postID="+post.postID()+"&amp;action=edit\">edit</a></dd>\n";
			sPost+="\t\t<dd><a href=\"postmessage?postID="+post.postID()+"&amp;action=delete\">delete</a></dd>\n";
			sPost+="\t\t</dl>\n";
		}
		
		sPost+="</footer></article>\n";
		return sPost;
	}

	public String printPostForm(int postID, int reqUserID, String action){
		MBPost post=new MBPost();
		Utilities utils=new abmb.utils.Utilities();
		if(postID!=-1){
			post=utils.getPostsByID(postID);
		}
		String form="";
		form+="<section>\n";
		form+="<h1>Post a message</h1>\n";
		form+="<form name=\"threadpost\" method=\"POST\" action=\""+PageLinks.getPostMessageLink()+"\">\n";
		form+="<input type=\"hidden\" name=\"postID\" value=\""+postID+"\" >";
		form+="<input type=\"hidden\" name=\"action\" value=\""+action+"\" >";
		form+="<fieldset>\n";
		form+="\t<label> Title:\n";
		form+="\t\t<input type=\"text\" id=\"title\" name=\"title\" required placeholder=\"Post title\" "+((post.title()!=null&&post.userID()==reqUserID)?"value=\""+post.title()+"\"":"")+">\n";
		form+="\t</label>\n";
		form+="\t<label> Post:\n";
		form+="\t\t<textarea id=\"threadbody\" name=\"threadbody\" maxlength=\"140\" placeholder=\"Post body\" required>"+((post.body()!=null&&post.userID()==reqUserID)?post.body():"")+"</textarea>\n";
		form+="\t</label>\n";
		form+="\t<label> Tags:</label>\n";
		if(reqUserID==post.userID()){
			if(post.tags().size()!=0){
				for(int i=1;i<=post.tags().size();++i){
					form+="\t\t<input type=\"text\" size=\"10\" id=\"tag"+i+"\" name=\"tag"+i+"\" placeholder=\"tag"+i+"\" value="+((post.tags().get(i-1)!=null&&post.userID()==reqUserID)?post.tags().get(i-1):"")+"> ";
				}
			}
			if(post.tags().size()<4){
				for(int i=post.tags().size()+1;i<=4;++i){
					form+="\t\t<input type=\"text\" size=\"10\" id=\"tag"+i+"\" name=\"tag"+i+"\" placeholder=\"tag"+i+"\"> ";
				}
			}
		}else{
			for(int i=1;i<=4;++i){
				form+="\t\t<input type=\"text\" size=\"10\" id=\"tag"+i+"\" name=\"tag"+i+"\" placeholder=\"tag"+i+"\"> ";
			}
		}
		form+="</fieldset>\n";
		form+="<fieldset>\n";
		form+="<input type=\"submit\" value=\"Post\">\n";
		form+="<input type=\"reset\" value=\"Reset\">\n";
		form+="</fieldset>\n";
		form+="</form>";
		form+="</section>\n";
		return form;
	}

	public String printPosts(ArrayList<MBPost> postList,int reqUserID){
		String postPrint="";
		postPrint+="<section>\n";
		if(postList.isEmpty()){
			postPrint+="No posts found\n";
		}
		else{
			for(MBPost post : postList){
				postPrint+=(printPost(post,reqUserID));
				postPrint+="\n";
			}
		}
		postPrint+="</section>\n";
		return postPrint;
	}

	private String printPostTitle(MBPost post) {
		DateFormat df=DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.MEDIUM, new Locale(System.getProperty("user.language")));
		String thr="";
		thr+="<article>\n";
		thr+="<header>\n";
		thr+="\t<time datetime=\""+new java.sql.Date(post.postDate().getTime())+"\" pubdate>"+df.format(new java.util.Date(post.postDate().getTime()))+"</time>\n";
		thr+="\t<h1><a href=\""+PageLinks.getShowPostLink()+"?postID="+post.postID()+"\">"+post.title()+"</a></h1>\n";
		thr+="</header>\n";
		thr+="\t\t<footer><dl>\n";
		thr+="\t\t\t <dt>Author:    </dt>\n";
		thr+="\t\t\t<dd><a href=\"index?keyword="+post.username()+"\">"+post.author()+"</a></dd>\n";
		thr+="\t\t\t<dt>Tags:      </dt>\n";
		for(int i=0;i<post.tags().size();++i){
			thr+="\t\t\t<dd><a href=\""+PageLinks.getLink("index")+"?tagkeyword="+post.tags().get(i)+"\">"+post.tags().get(i)+"</a></dd>\n";
		}
		thr+="\t\t</dl></footer>\n";
		thr+="</article>\n";
		return thr;
	}
	public String printSearchForm(){
		String form="";
		form+="<div class=\"c1\"><section> \n";
		form+="<h1>Search</h1>\n";
		form+="\t <form name=\"search\" method=\"POST\" action=\""+PageLinks.getIndexLink()+"\">\n";
		form+="\t\t <label> Keyword:\n";
		form+="\t\t\t<input type=\"search\" title=\"Type at least 1 character\" autocomplete=\"on\" placeholder=\"post, tag, user, ...\" name=\"keyword\" required maxlength=\"50\">\n";
		form+="\t\t</label>\n";
		form+="\t\t<input type=\"submit\" value=\"Search\">\n";
		form+="\t</form>\n";
		form+="</section></div>\n";
		return form;
	}
	public String printSidebar(boolean loggedin){
		ArrayList<String> sideNavLinks=PageLinks.getNavLinks(loggedin);
		String sideBar="";
		sideBar+="<aside>\n";
		if(loggedin){
			sideBar+=(printSearchForm());
		}
		sideBar+="<nav>\n";
		sideBar+="<h1>Links</h1>\n";
		sideBar+="<ul>\n";
		for(String link:sideNavLinks){
			sideBar+="<li>"+link+"</li>\n";
		}
		sideBar+="</ul>\n";
		sideBar+="</nav>\n";
		sideBar+="</aside>\n";
		return sideBar;
	}
	public String printUserSearchForm() {

		String form="";
		form+="<section>\n";
		form+="<form action=\""+PageLinks.getFollowLink()+"\" method=\"post\">\n";
		form+="<fieldset>\n";
		form+="<label>Search for users: \n";
		form+="\t<input type=\"search\" name=\"uSearch\" placeholder=\"user\" required></label>\n";
		form+="</fieldset>\n";
		form+="<fieldset>\n";
		form+="\t<input type=\"submit\" value=\"Search\">\n";
		form+="</fieldset>";
		form+="</form>";
		form+="</section>";
		return form;

	}

}
